package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/subscribe")
public class SubscribeController {

    @Reference
    private OrderService orderService;

    /**
     * 带条件分页查询检查组信息
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody Map<String,String> queryPageBean){
        try {
            PageResult result = orderService.selectByCondition(queryPageBean);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 添加预约
     * @param map 预约数据
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Map<String,String> map,@RequestParam("setmealId") String setmealId)
    {
        try
        {
            map.put("setmealId",setmealId);
            map.put("orderType","电话预约");
            return orderService.saveOrderInfo(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new Result(false,"系统出错，请联系技术人员");
        }
    }

    /**
     * 根据id查询数据并回显
     * @param orderId
     * @return
     */
    @GetMapping("/findById")
    public Result add(Integer orderId){
        try{
            Map<String,Object>  map= orderService.findById(orderId);
            return new Result(true,"成功",map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new Result(false,"系统出错，请联系技术人员");
        }
    }

    /**
     * 修改预约
     * @param map
     * @return
     */
    @PostMapping("/edit")
    public Result eidt(@RequestBody Map<String,String> map,Integer setmealId)
    {
        try{
            map.put("orderType","电话预约");
            map.put("setmealId",setmealId.toString());
            return orderService.editOrderInfo(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new Result(false,"系统出错，请联系技术人员");
        }
    }

    /**
     * 修改到诊状态
     * @param map
     */
    @PostMapping("/status")
    public void pay(@RequestBody Map<String,Object> map){
             orderService.changeStatus(map);
    }
}
